<?php
class Webkul_Marketplacepartner_Block_Partners_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct(){
        parent::__construct();
        $this->setId('customerGrid');
        $this->setUseAjax(true);
        $this->setDefaultSort('entity_id');
        $this->setSaveParametersInSession(true);
    }
    protected function _prepareCollection(){
		$mysqlprefix = Mage::getConfig()->getTablePrefix();//code for mysql prefix
		$mytablepartnerstatus=$mysqlprefix.'marketplacepartner_entity_userdata';
		$mytablepayment=$mysqlprefix.'marketplacepartner_entity_datapayment';
		$collectionfetch = Mage::getModel('marketplaceprofile/marketplaceprofile')->getCollection();
		$record = array();
		foreach($collectionfetch as $id)
		{
			$record[] = $id->getmageuserid();
		}
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
		$collection->addAttributeToFilter('entity_id', array('in' => $record));
		$collection->getSelect()->joinLeft($mytablepartnerstatus,'e.entity_id = '.$mytablepartnerstatus.'.mageuserid',array('partnerstatus'=>'partnerstatus'));
		$collection->getSelect()->joinLeft($mytablepayment,'e.entity_id = '.$mytablepayment.'.userid',array('paymentsource'=>'paymentsource'));
		foreach($collection as $coll)
		$coll->sellerstatus = sprintf('<span class="status" data="'.$this->getUrl('sellerstatus/adminhtml_sellerstatus/sellerstatus').'" value="'.$coll->getId().'"><button>Download Status</button></span>');
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns(){
        $this->addColumn('entity_id', array(
            'header'    => Mage::helper('customer')->__('ID'),
            'width'     => '50px',
            'index'     => 'entity_id',
            'type'  => 'number',
			'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('name', array(
            'header'    => Mage::helper('customer')->__('Name'),
            'index'     => 'name',
			'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('email', array(
            'header'    => Mage::helper('customer')->__('Email'),
            'width'     => '150',
            'index'     => 'email',
			'filter'    => false,
            'sortable'  => false
        ));
		// for partner status 
		$this->addColumn('partnerstatus', array(
            'header'    => Mage::helper('customer')->__('Partner Status'),
            'index'     => 'partnerstatus',
			'filter'    => false,
            'sortable'  => false
        ));
		$this->addColumn('paymentsource', array(
            'header'    => Mage::helper('customer')->__('Mode Of Payment'),
            'index'     => 'paymentsource',
			'filter'    => false,
            'sortable'  => false
        ));
		$isThisEnabled = Mage::helper('core/data')->isModuleOutputEnabled('Webkul_Sellerstatus');
		if($isThisEnabled == 1){
		$this->addColumn('sellerstatus', array(
            'header'    => Mage::helper('customer')->__('Seller Status'),
			'type'      => 'text',
			'index'     => 'sellerstatus',
			'filter'    => false,
            'sortable'  => false
        ));
		}
		// for partner status 
        $this->addColumn('Telephone', array(
            'header'    => Mage::helper('customer')->__('Telephone'),
            'width'     => '100',
            'index'     => 'billing_telephone',
			'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('billing_postcode', array(
            'header'    => Mage::helper('customer')->__('ZIP'),
            'width'     => '90',
            'index'     => 'billing_postcode',
			'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('billing_country_id', array(
            'header'    => Mage::helper('customer')->__('Country'),
            'width'     => '100',
            'type'      => 'country',
            'index'     => 'billing_country_id',
			'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('billing_region', array(
            'header'    => Mage::helper('customer')->__('State/Province'),
            'width'     => '100',
            'index'     => 'billing_region',
			'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('customer_since', array(
            'header'    => Mage::helper('customer')->__('Customer Since'),
            'type'      => 'datetime',
            'align'     => 'center',
            'index'     => 'created_at',
            'gmtoffset' => true,
			'filter'    => false,
            'sortable'  => false
        ));
        if (!Mage::app()->isSingleStoreMode()) {
            $this->addColumn('website_id', array(
                'header'    => Mage::helper('customer')->__('Website'),
                'align'     => 'center',
                'width'     => '80px',
                'type'      => 'options',
                'options'   => Mage::getSingleton('adminhtml/system_store')->getWebsiteOptionHash(true),
                'index'     => 'website_id',
            ));
        }
        return parent::_prepareColumns();
    }

    protected function _prepareMassaction(){
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('customer');
		$this->getMassactionBlock()->addItem('ispartner', array(
             'label'    => Mage::helper('customer')->__('Is Partner'),
             'url'      => $this->getUrl('*/*/massispartner')
        ));
		$this->getMassactionBlock()->addItem('isnotpartner', array(
             'label'    => Mage::helper('customer')->__('Is Not Partner'),
             'url'      => $this->getUrl('*/*/massnotpartner')
        ));
		$isThisEnabled = Mage::helper('core/data')->isModuleOutputEnabled('Webkul_Sellerstatus');
		if($isThisEnabled == 1){
			$this->getMassactionBlock()->addItem('sellesstatus', array(
				 'label'    => Mage::helper('customer')->__('Download Status'),
				 'url'      => $this->getUrl('sellerstatus/sellerstatus/masssellerstatus')
			));
		}
        return $this;
        return $this;
    }
}
